package org.epanetgrid.otimizacao.replicador;

import org.epanetgrid.otimizacao.simulation.GradienteNaoConvergeOtimizacaoException;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Operator;
import org.jheuristics.ga.Population;
import org.jheuristics.ga.operators.PopulationChooser;


/**
 * Operador que realiza a replica��o 
 * @author vinicius
 *
 */
public class ReplicadorOperator implements Operator {

	private PopulationChooser chooser;
	
	private IIndividualReplicator replicator;
	
	/**
	 * @param chooser Objeto que selecionar� a popula��o que ser� replicada
	 * @param replicator Replicador da popula��o
	 */
	public ReplicadorOperator(PopulationChooser chooser, IIndividualReplicator replicator) {
		this.chooser = chooser;
		this.replicator = replicator;
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.Operator#operate(org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public void operate(GAStatus status, GAConfig config) {
		Population pop = chooser.choose(status, config);
		
		Population novaPop = replicator.replique(pop);
		
		
		if(novaPop.size() != pop.size()) {
			throw new GradienteNaoConvergeOtimizacaoException();
		}
		
		pop.clear();
		pop.addIndividuals(novaPop);
	}

}
